/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.sinodata.bsm.center.dao.BaseDao;
import com.sinodata.bsm.center.dao.ReportDao;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-8-16 下午3:49:29          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
@Repository
public class ReportDaoImpl extends BaseDao implements ReportDao {

    private static final Logger logger = Logger.getLogger(ReportDaoImpl.class);

    public List<String[]> getReportData(String tableName, String query) {
        List<String[]> list = new ArrayList<String[]>();
        PreparedStatement pStmt = null;
        ResultSet rs = null;
        ResultSetMetaData rsMetaData = null;

        try {
            pStmt = getSession().connection().prepareStatement("select * from " + tableName + query);
            rs = pStmt.executeQuery();
            rsMetaData = rs.getMetaData();
            int numberOfColumns = rsMetaData.getColumnCount();
            while (rs.next()) {
                String[] strAry = new String[numberOfColumns];
                for (int i = 1; i <= numberOfColumns; i++) {
                    strAry[i - 1] = rs.getString(i);
                }
                list.add(strAry);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e);
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e2) {
                }
            }
            if (pStmt != null) {
                try {
                    pStmt.close();
                } catch (Exception e2) {
                }
            }
        }
        return list;
    }

}
